package floyd;

import java.util.Arrays;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class Floyd {

    public static void main(String[] args) {
        final int N = 65535;
        char[] vertexs = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int[][] matrix = new int[][]{
                {0, 5, 7, N, N, N, 2},
                {5, 0, N, 9, N, N, 3},
                {7, N, 0, N, 8, N, N},
                {N, 9, N, 0, N, 4, N},
                {N, N, 8, N, 0, 5, 4},
                {N, N, N, 4, 5, 0, 6},
                {2, 3, N, N, 4, 6, 0}
        };
        Graph graph = new Graph(7, matrix, vertexs);
        graph.floyd();
        graph.show();


    }

}

class Graph {
    private char[] vertex;
    private int[][] dis;
    private int[][] pre;

    public Graph(int length, int[][] matrix, char[] vertex) {
        this.vertex = vertex;
        this.dis = matrix;
        this.pre = new int[length][length];
        for (int i = 0; i < pre.length; i++) {
            Arrays.fill(pre[i], i);
        }
    }

    public void floyd() {
        int len = 0;
        //中间顶点
        for (int k = 0; k < dis.length; k++) {
            //出发顶点
            for (int i = 0; i < dis.length; i++) {
                //目标顶点
                for (int j = 0; j < dis.length; j++) {
                    len = dis[i][k] + dis[k][j]; //从i到k的距离+ 从k到j的距离
                    if (len < dis[i][j]) {
                        dis[i][j] = len;
                        pre[i][j] = pre[k][j];
                    }
                }
            }
        }
    }

    public void show() {
        System.out.println("======dis数组==========");
        for (int i = 0; i < dis.length; i++) {
            for (int j = 0; j < dis.length; j++) {
                System.out.print(dis[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("======pre数组==========");
        for (int i = 0; i < dis.length; i++) {
            for (int j = 0; j < dis.length; j++) {
                System.out.print(pre[i][j] + " ");
            }
            System.out.println();
        }

    }
}
